package com.lanou.dllo.shise.com.lanou.dllo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.entity.FoodZineBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/31.
 */
public class Food_magazineListAdapter extends RecyclerView.Adapter<Food_magazineListAdapter.ViewHolder> {
    private List<FoodZineBean.ResultEntity> food;
    private RequestQueue queue;
    public Food_magazineListAdapter(Context context){
        queue = Volley.newRequestQueue(context);
    }
    public void addData(List<FoodZineBean.ResultEntity> datas){
        food = datas;
        notifyDataSetChanged();
    }
    @Override
    public Food_magazineListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_food_magazine,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Food_magazineListAdapter.ViewHolder holder, int position) {
        if (food != null){
            FoodZineBean.ResultEntity f = food.get(position);
            holder.magazine_user_name.setText(f.getUser_name());
            holder.magazine_serial_number.setText(f.getSerial_number());
            holder.foodzine_name.setText(f.getFoodzine_name());
            if (f.getUser_image() != null && f.getUser_image().length() > 0){
                String url = f.getUser_image();
                getBitmap(url,holder.magazine_user_image);
            }
            if (f.getPicture_url() != null && f.getPicture_url().length() > 0){
                String path = f.getPicture_url();
                getBitmap(path,holder.magazine_picture_url);
            }
        }

    }

    private void getBitmap(String path, final ImageView magazine_picture_url) {
        ImageRequest request = new ImageRequest(path, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                magazine_picture_url.setImageBitmap(bitmap);
            }
        }, 720, 1280, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
                return super.getHeaders();
            }
        };
        queue.add(request);
    }

        @Override
    public int getItemCount() {
        if (food != null){
            return food.size();
        }
        return 0;
    }

     class ViewHolder extends RecyclerView.ViewHolder {
         private ImageView magazine_picture_url,magazine_user_image;
         private TextView magazine_serial_number,foodzine_name,magazine_user_name;
         public ViewHolder(View itemView) {
             super(itemView);
             magazine_picture_url = (ImageView) itemView.findViewById(R.id.magazine_picture_url);
             magazine_user_image = (ImageView) itemView.findViewById(R.id.magazine_user_image);
             magazine_serial_number = (TextView) itemView.findViewById(R.id.magazine_serial_number);
             foodzine_name = (TextView) itemView.findViewById(R.id.foodzine_name);
             magazine_user_name = (TextView) itemView.findViewById(R.id.magazine_user_name);
         }
     }
}
